package com.collect.cameroon.zhiNetApi;

import android.text.TextUtils;

import com.collect.cameroon.zhiUtils.MyLogger;

import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class ZHIHttpRequestThread {
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static void createRequestThread(final String url, final String info) throws Exception {
        if(TextUtils.isEmpty(url)){return;}
        if(TextUtils.isEmpty(info)){return;}

        Thread thread =  new Thread(() -> {
            OkHttpClient okHttpClient = ZHIHttpApiProvider.genericClient();
            RequestBody body = RequestBody.create(JSON, info);

            final Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();
            Call call = okHttpClient.newCall(request);
            try {
                String result = Objects.requireNonNull(call.execute().body()).string();
                MyLogger.e(ZHIHttpRequestThread.class.getSimpleName(),  result);
            } catch (IOException e) {
                MyLogger.e(ZHIHttpRequestThread.class.getSimpleName(),  "上传信息失败" +  e.toString());
            }
        });
        thread.start();
    }


    public void addPreCreditConfirmEvent(String type, String credit) {
//        try {
//            if (TextUtils.isEmpty(type)) {
//                return;
//            }
//            if (TextUtils.isEmpty(credit)) {
//                return;
//            }
//
//            String token = StringUtil.safeString(ZHIMainProcessHelper.getInstance().getToken());
//
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("type", type);
//            jsonObject.put("remark", credit);
//            jsonObject.put("androidId", MONEYDeviceDiper.getInstance().getAndroidID());
//            jsonObject.put("advertId", MONEYGoogleServiceDiper.getInstance().getGoogleAdvertId());
//            jsonObject.put("version", MONEYProjectConfigs.APP_VERSION);
//            jsonObject.put("product", MONEYProjectConfigs.APP_TYPE);
//            jsonObject.put("token", token);
//
//            final String jsonInfo = jsonObject.toString();
//            final String url = ZHIProjectConfigs.DEFAULT_URL + "/kml-front/v1/userBuriedPoint";
//            try {
//                MONEYNetApiThread.createRequestThread(url, jsonInfo);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }

}
